package com.minisoft.pms;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

public class WorkflowClientManual {

    private static final String SERVICE_URL = "http://172.16.51.245:88/services/InitProcessInterface";
    private static final String NAMESPACE_URI = "webservice.InitProcess.westvalley.com";

    public static void main(String[] args) {
        // 构建SOAP消息体
        String xmlContent = "<workflowRequestInfo>\n" +
                "    <saplcbh>f519215dc52a434dbd7899f62dcbcb90</saplcbh>\n" +
                "    <saplclx>PMS</saplclx>\n" +
                "    <creatorId>linshuying</creatorId>\n" +
                "    <isNext>0</isNext>\n" +
                "    <requestName>华发数智项目变更审批表</requestName>\n" +
                "    <workflowId>22442</workflowId>\n" +
                "</workflowRequestInfo>"; // 实际XML内容
        // 对 xmlContent 中的特殊字符进行XML转义，但不是URL编码
        String escapedXmlContent = xmlContent;
        String soapEnvelope =
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "xmlns:web=\"" + NAMESPACE_URI + "\">" +
                "<soapenv:Header/>" +
                "<soapenv:Body>" +
                    "<web:createOrUpdateWorkflow>" +
                        "<xml>" + escapedXmlContent + "</xml>" + // 直接插入已转义的XML内容
                    "</web:createOrUpdateWorkflow>" +
                "</soapenv:Body>" +
            "</soapenv:Envelope>";

        // 设置HTTP头信息
        HttpRequest request = HttpRequest.post(SERVICE_URL+"/createOrUpdateWorkflow")
            .header("Content-Type", "text/xml; charset=utf-8")
            .header("SOAPAction", ""); // 如果需要的话，设置SOAP Action

        // 发送请求
        HttpResponse response = request.body(soapEnvelope).execute();

        // 处理响应
        if (response.isOk()) {
            String responseBody = response.body();
            System.out.println("Response Body: " + responseBody);

            // 解析返回的XML（这里省略了解析逻辑）
            // 可以使用Hutool的XmlUtil或者第三方库如JAXB、Jackson等进行解析
        } else {
            System.err.println("Error occurred while calling the service.");
            System.err.println("Status code: " + response.getStatus());
            System.err.println("Response body: " + response.body());
        }
    }
}